﻿/////////////////////////////////////////////////////////////////////////////////////////////
// <copyright file="OutlookLiveGroups.cs" company="Microsoft Corporation">
//     Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
/////////////////////////////////////////////////////////////////////////////////////////////

namespace Microsoft.LiveAtEdu.Samples.Powershell.OutlookLive
{
    using System;
    using System.Management.Automation;

    /// <summary>
    /// Powershell commands used by groups.
    /// </summary>
    public class OutlookLiveGroups
    {
        /// <summary>
        /// Session runspace instance.
        /// </summary>
        private readonly SessionRunspace sessionRunspace;

        /// <summary>
        /// Initializes a new instance of the OutlookLiveGroups class.
        /// </summary>
        /// <param name="sessionRunspace">Session runspace instance.</param>
        public OutlookLiveGroups(SessionRunspace sessionRunspace)
        {
            if (sessionRunspace == null)
            {
                throw new ArgumentOutOfRangeException("sessionRunspace");
            }

            this.sessionRunspace = sessionRunspace;
        }

        /// <summary>
        /// Method to create Dynamic distribution list (DDL)
        /// </summary>
        /// <param name="ddlName">Name of the DDL</param>
        /// <param name="filterExp">filter expression</param>
        /// <returns>true if success</returns>
        public bool CreateDDL(string ddlName, string filterExp)
        {
            if (string.IsNullOrEmpty(ddlName))
            {
                throw new ArgumentOutOfRangeException("ddlName");
            }

            if (string.IsNullOrEmpty(filterExp))
            {
                throw new ArgumentOutOfRangeException("filterExp");
            }

            try
            {
                var command = new PSCommand();
                command.AddScript(string.Format(GroupsScripts.CreateDdlScript, ddlName, filterExp));
                this.sessionRunspace.ExecuteCommand(command);
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}
